#include "touch.h"


Touch* Touch::m_instance = NULL;

Touch::Touch(enum BoardType type,const char *deviceName):m_point(0,0)
{
    memcpy(m_deviceName,deviceName,strlen(deviceName));
    m_type = type;
}

Touch::~Touch()
{

}

int Touch::wait(int &rx, int &ry)
{
    int tsfd = open("/dev/input/event0",O_RDWR);
    if(tsfd == -1)
    {
        printf("open /dev/input/event0 error\n");
        return -1;
    }

    while(1)
    {
        struct input_event info;

        //3）读取触摸屏数据
        read(tsfd,&info,sizeof(struct input_event));

        //判断当前发生的是触摸屏事件,接着判断触发的是X轴事件
        if(info.type == EV_ABS && info.code == ABS_X)
        {
            m_point.setX(info.value);
        }
        //判断当前发生的是触摸屏事件,接着判断触发的是Y轴事件
        if(info.type == EV_ABS && info.code == ABS_Y)
        {
            m_point.setY(info.value);
        }

        //松开的时候，才进行打印  type:1 code:330 value:0
        if(info.type == EV_KEY && info.code == BTN_TOUCH &&  info.value == 0)
        {

            //如果你的板子是黑色的，那么需要进行坐标转换(1024,600) ---(800,480)
            if(m_type == BOARD_TYPE_GEC6818_BLACK)
            {
                m_point.setX((m_point.x() *1.0)/1024 * 800);
                m_point.setY((m_point.y() *1.0)/600  * 480);
            }

            rx = m_point.x();
            ry = m_point.y();
            std::cout<<"x:"<<rx<<" y:"<<ry<<std::endl;

            break;
        }
    }

    //5）关闭触摸屏文件
    close(tsfd);
}

int Touch::wait(Point &val)
{
    int tsfd = open("/dev/input/event0",O_RDWR);
    if(tsfd == -1)
    {
        printf("open /dev/input/event0 error\n");
        return -1;
    }

    while(1)
    {
        struct input_event info;

        //3）读取触摸屏数据
        read(tsfd,&info,sizeof(struct input_event));

        //判断当前发生的是触摸屏事件,接着判断触发的是X轴事件
        if(info.type == EV_ABS && info.code == ABS_X)
        {
            m_point.setX(info.value);
        }
        //判断当前发生的是触摸屏事件,接着判断触发的是Y轴事件
        if(info.type == EV_ABS && info.code == ABS_Y)
        {
            m_point.setY(info.value);
        }

        //松开的时候，才进行打印  type:1 code:330 value:0
        if(info.type == EV_KEY && info.code == BTN_TOUCH &&  info.value == 0)
        {

            //如果你的板子是黑色的，那么需要进行坐标转换(1024,600) ---(800,480)
            if(m_type == BOARD_TYPE_GEC6818_BLACK)
            {
                m_point.setX((m_point.x() *1.0)/1024 * 800);
                m_point.setY((m_point.y() *1.0)/600  * 480);
            }

            std::cout<<"x:"<<m_point.x()<<" y:"<<m_point.y()<<std::endl;

            val = m_point;
            break;
        }
    }

    //5）关闭触摸屏文件
    close(tsfd);
}

Point Touch::wait()
{
    int tsfd = open("/dev/input/event0",O_RDWR);
    if(tsfd == -1)
    {
        printf("open /dev/input/event0 error\n");
        return -1;
    }

    while(1)
    {
        struct input_event info;

        //3）读取触摸屏数据
        read(tsfd,&info,sizeof(struct input_event));

        //判断当前发生的是触摸屏事件,接着判断触发的是X轴事件
        if(info.type == EV_ABS && info.code == ABS_X)
        {
            m_point.setX(info.value);
        }
        //判断当前发生的是触摸屏事件,接着判断触发的是Y轴事件
        if(info.type == EV_ABS && info.code == ABS_Y)
        {
            m_point.setY(info.value);
        }

        //松开的时候，才进行打印  type:1 code:330 value:0
        if(info.type == EV_KEY && info.code == BTN_TOUCH &&  info.value == 0)
        {

            //如果你的板子是黑色的，那么需要进行坐标转换(1024,600) ---(800,480)
            if(m_type == BOARD_TYPE_GEC6818_BLACK)
            {
                m_point.setX((m_point.x() *1.0)/1024 * 800);
                m_point.setY((m_point.y() *1.0)/600  * 480);
            }

            std::cout<<"x:"<<m_point.x()<<" y:"<<m_point.y()<<std::endl;

            break;
        }
    }

    //5）关闭触摸屏文件
    close(tsfd);

    return m_point;
}

int Touch::x()
{
    return m_point.x();
}

int Touch::y()
{
    return m_point.y();
}

Point Touch::point()
{
    return m_point;
}

Touch::BoardType Touch::boardType()
{
    return m_type;
}

void Touch::setBoardType(Touch::BoardType type)
{
    m_type = type;
}
Touch *Touch::intstance(enum BoardType type)
{
    if(m_instance == NULL)
    {
        m_instance = new Touch(type);
    }

    return m_instance;
}

Point::Point(int x, int y)
{
    this->m_x = x;
    this->m_y = y;
}

Point::~Point()
{

}

int Point::x()
{
    return this->m_x;
}

void Point::setX(int x) //setX(Point *this,int x)
{
    this->m_x = x;
}

int Point::y()
{
    return this->m_y;
}

void Point::setY(int y)
{
    this->m_y = y;
}

void Point::setXY(int x, int y)
{
    this->m_x = x;
    this->m_y = y;
}

Point Point::pointXY()
{
    return *this;
}



